package model;

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.ArrayList;

import physics.Angle;
import physics.Geometry;
import physics.LineSegment;
import physics.Vect;

public class TriangleBumper implements IGizmo {
	
	private String name;
	private int L;
	private int x;
	private int y;
	private Color col;
	private LineSegment[] lines;
	private physics.Circle[] corners;
	private Vect reflection = null;
	
	int rotationSetting;
		
	public TriangleBumper (int gridL) {
		L = gridL;
		col = new Color(0x0000FF);
	}
	
	public TriangleBumper (int gridL, String s) {
		name = s;
		L = gridL;
		col = new Color(0x0000FF);
	}
		
	public void setPosition(int xpos, int ypos) {
		x = xpos*L;
		y = ypos*L;
		
		lines = new LineSegment[3];
		lines[0] = new LineSegment(x, y, x+L, y);		//top
		lines[1] = new LineSegment(x+L, y, x+L, y+L);	//right
		lines[2] = new LineSegment(x+L, y+L, x, y);		//hyp
		corners = new physics.Circle[3];
		corners[0] = new physics.Circle(x, y, 0);
		corners[1] = new physics.Circle(x+L, y, 0);
		corners[2] = new physics.Circle(x+L, y+L, 0);
		rotateClockwise();
		rotateClockwise();
		rotateClockwise();
		rotationSetting = 0;
		
		
	}
	
	@Override
	public void addLinkedGizmo(IGizmo giz) {
		// TODO Auto-generated method stub
	}	
	
	@Override
	public void addLinkedKey(int key) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doLinkedAction() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Rectangle getBounds() {
		return new Rectangle(x, y, L, L);
	}

	@Override
	public Color getColor() {
		return col;
	}

	@Override
	public ArrayList<IGizmo> getLinkedGizmos() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ArrayList<Integer> getLinkedKeys() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getType() {
		return "Triangle";
	}

	@Override
	public int getX() {
		return x;
	}

	@Override
	public int getY() {
		return y;
	}

	@Override
	public Graphics paintShape(Graphics g) {
		g.setColor(col);
		Polygon p = new Polygon();
		for (LineSegment each : lines)
			p.addPoint((int)each.p1().x(), (int)each.p1().y());
		g.fillPolygon(p);		
		return g;
	}

	@Override
	public void rotateClockwise() {
		double x = this.x + (double)L/2;
		double y = this.y + (double)L/2;
		Vect centre = new Vect(x, y);
		for (int i=0 ; i < lines.length ; i++)
			lines[i] = Geometry.rotateAround(lines[i], centre, Angle.DEG_90);
		for (int i=0 ; i < corners.length ; i++)
			corners[i] = Geometry.rotateAround(corners[i], centre, Angle.DEG_90);
		if (rotationSetting == 3) rotationSetting = 0;
		else rotationSetting++;
		System.out.print(rotationSetting);
	}

	@Override
	public void setColor(Color col) {
		this.col = col;
	}

	@Override
	public void triggerEvent() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public double timeUntilCollision(Ball ball) {
		double time=Double.POSITIVE_INFINITY, temp=0;
		
		for (LineSegment line : lines) {
			temp = Geometry.timeUntilWallCollision(line, ball.getCircle(), ball.getV());
			if (temp < time) {
				time = temp;
				reflection = Geometry.reflectWall(line, ball.getV()); 
			}
		}
		
		for (physics.Circle corner : corners) {
			temp = Geometry.timeUntilCircleCollision(corner, ball.getCircle(), ball.getV());
			if (temp < time) {
				time = temp;
				reflection = Geometry.reflectCircle(corner.getCenter(), ball.getCircle().getCenter(), ball.getV());
			}
		}
		return time;
	}

	@Override
	public Vect reflectionVector(Ball ball) {
		return reflection;
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public int getTriangleRotations() {
		// TODO Auto-generated method stub
		return rotationSetting;
	}
	
}